package com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.kongzue.dialog.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialog.util.BaseDialog;
import com.kongzue.dialog.v3.MessageDialog;
import com.kongzue.dialog.v3.WaitDialog;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.data.entity.EventMessage;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.VipContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.VipPresenter;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.VipAdapter;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;
import com.ninetaildemonfox.zdl.socialproject.utils.dialog.adapter.PayAdapter;
import com.ninetaildemonfox.zdl.socialproject.utils.dialog.bean.PayNamePayBean;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author NineTailDemonFox
 * @date 2019/7/23 16:01
 * 功能描述： vip界面
 * 联系方式：1037438704@qq.com
 */

public class VipActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener, VipContract {
    @BindView(R.id.image_left_finish)
    ImageView imageleftFinish;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_money)
    TextView text_money;
    @BindView(R.id.text_gone)
    TextView textGone;
    @BindView(R.id.text_vip_look)
    TextView text_vip_look;
    @BindView(R.id.text_right)
    TextView textRight;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.recyclerViewPay)
    RecyclerView recyclerViewPay;
    @BindView(R.id.text_open_immediately)
    TextView textOpenImmediately;
    @BindView(R.id.text_top_gone)
    TextView textTopGone;
    private List<PayNamePayBean> list;
    private PayAdapter payAdapter;
    private String count;
    private VipAdapter systemNewsAdapter;
    private VipPresenter vipPresenter;
    int payCount = -1;
    private String price;
    private String meal_id;
    public int weixin = 0;


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_vip;
    }

    @Subscribe
    @Override
    public void onInitialization(Bundle bundle) {
        vipPresenter = new VipPresenter(this, me);
        EventBus.getDefault().register(this);
        weixin = getIntent().getExtras().getInt("weixin");
        initView();
        initData();
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventMessage event) {
        //弹吐司显示EventBusActivity中提交事件的信息
        if (weixin == 10) {
            startActivity(new Intent(me, MainActivity.class));
        }

    }


    @Override
    protected void onResume() {
        super.onResume();
        WaitDialog.show(me, "数据加载中");
        vipPresenter.getMealList(token);
    }

    private void initView() {
        // 是否vip 1否 2是
        count = getIntent().getExtras().getString("count");

        if (getIntent().getExtras().getString("vip_time") != null) {
            String vip_time = getIntent().getExtras().getString("vip_time");
            textTopGone.setText("您已开通会员,有效期至" + vip_time);
        }
        if (count.equals("1")) {
            textGone.setVisibility(View.GONE);
            textTopGone.setVisibility(View.GONE);
        } else {
            textOpenImmediately.setVisibility(View.VISIBLE);
        }

        list = new ArrayList<>();
        textCenter.setText("会员中心");
        imageleftFinish.setVisibility(View.VISIBLE);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        recyclerViewPay.setLayoutManager(new LinearLayoutManager(this));
        systemNewsAdapter = new VipAdapter(R.layout.list_vip_adp);
        recyclerView.setAdapter(systemNewsAdapter);
        systemNewsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<Map<String, String>> data = systemNewsAdapter.getData();
                for (int i = 0; i < data.size(); i++) {
                    data.get(i).put("count", "1");
                }
                data.get(position).put("count", "2");
                text_money.setText(data.get(position).get("total") + "元");
                systemNewsAdapter.notifyDataSetChanged();
            }
        });
    }

    private void initData() {
        payAdapter = new PayAdapter(R.layout.list_dialog_pay);
        recyclerViewPay.setAdapter(payAdapter);
        list.add(new PayNamePayBean("钱包支付", false, R.mipmap.ci_pay_wallet, 1));
        list.add(new PayNamePayBean("支付宝支付", false, R.mipmap.ic_pay_alipay, 2));
        list.add(new PayNamePayBean("微信支付", false, R.mipmap.ic_pay_wx, 3));
        payAdapter.setNewData(list);
        payAdapter.setCount(1);
        payAdapter.setOnItemClickListener(this);
    }

    @OnClick({R.id.image_left_finish, R.id.text_open_immediately})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_left_finish:
                finish();
                break;
            case R.id.text_open_immediately:
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getChoice() == true) {
                        payCount = i;
                    }
                }
                if (payCount == -1) {
                    ToastUtil.show("请选择支付方式");
                    return;
                }
                List<Map<String, String>> data = systemNewsAdapter.getData();
                for (int i = 0; i < data.size(); i++) {
                    if (data.get(i).get("count").equals("2")) {
                        price = data.get(i).get("total");
                        meal_id = data.get(i).get("id");
                    }
                }
                if (price == null) {
                    ToastUtil.show("请选择套餐！");
                    return;
                }
                //立即开通
                MessageDialog.show(me, "使用" + list.get(payCount).name, "", "确认", "取消")
                        .setOnOkButtonClickListener(new OnDialogButtonClickListener() {
                            @Override
                            public boolean onClick(BaseDialog baseDialog, View v) {
                                if (payCount == 0) {
//                                    钱包支付
                                    WaitDialog.show(me, "数据请求中");
                                    vipPresenter.balancePay(token, "", "5", price, meal_id);
                                } else if (payCount == 1) {
//                                    支付宝支付
                                    vipPresenter.aliPay(token, "", "5", price, meal_id);
                                } else {
//                                    微信支付
                                    vipPresenter.wxPay(token, "", "5", price, meal_id);
                                }
                                return false;
                            }
                        });
                break;
            default:
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        List<PayNamePayBean> data = payAdapter.getData();
        PayNamePayBean payNamePayBean = data.get(position);
        if (payNamePayBean.getChoice() == true) {
            payNamePayBean.setChoice(false);
        } else {
            for (int i = 0; i < data.size(); i++) {
                data.get(i).setChoice(false);
            }
            payNamePayBean.setChoice(true);
        }
        payAdapter.notifyDataSetChanged();
    }


    public static void start(Context me, String count) {
        Bundle bundle = new Bundle();
        bundle.putString("count", count);
        Intent intent = new Intent(me, VipActivity.class);
        intent.putExtras(bundle);
        me.startActivity(intent);
    }

    public static void start(Context me, String count, int weixin) {
        Bundle bundle = new Bundle();
        bundle.putString("count", count);
        bundle.putInt("weixin", weixin);
        Intent intent = new Intent(me, VipActivity.class);
        intent.putExtras(bundle);
        me.startActivity(intent);
    }


    public static void start(Context me, String count, String vip_time) {
        Bundle bundle = new Bundle();
        bundle.putString("count", count);
        bundle.putString("vip_time", vip_time);
        Intent intent = new Intent(me, VipActivity.class);
        intent.putExtras(bundle);
        me.startActivity(intent);
    }

    @Override
    public void Success(Map<String, String> data) {
        Map<String, String> map = JSONUtils.parseKeyAndValueToMap(data.get("data"));

       String vip_look = "每天"+map.get("vip_look")+"次免费机会看社交账号";
        text_vip_look.setText(vip_look);
        ArrayList<Map<String, String>> datalist = JSONUtils.parseKeyAndValueToMapList(map.get("list"));
        if (datalist != null) {
            for (int i = 0; i < datalist.size(); i++) {
                datalist.get(i).put("count", "1");
            }
            systemNewsAdapter.setNewData(datalist);
        }


        if (count.equals("1")) {
            textGone.setVisibility(View.GONE);
            textTopGone.setVisibility(View.GONE);
        } else {
        }

    }

    @Override
    public void Failed(String msg) {

    }

    @Override
    public void showLoading() {
        WaitDialog.show(me, "数据加载中");
    }
}